(function(){
	var applyId = $('#applyPageInfoForm input[name=applyId]').val();
	function initApplyInfo(){
		$.ajax({
	        url: ctx + '/apply/selectAllByWhere',
	        data:{"applyId":applyId},
	        type: 'POST',
	        dataType: 'json',
	        success: function (data) {
	        		data = data.returnObject.aaData;
	        		$(data).each(function(i,item){
	        			$('#financeApplyForm input[name=applicantName]').val(item.applicantName);
	        			$('#financeApplyForm input[name=applicantDate]').val(item.applicantDate);
	        			$('#financeApplyForm input[name=money]').val(item.money);
	        			$('#financeApplyForm select[name=paymentFrom]').val(item.paymentFrom);
	        			$('#financeApplyForm select[name=payment]').val(item.payment);
	        			$('#financeApplyForm input[name=expendDetail]').val(item.expendDetail);
	        			$('#financeApplyForm input[name=invoiceTitle]').val(item.invoiceTitle);
	        			$('#financeApplyForm input[name=departmentName]').val(item.departmentName);
	        			$('#financeApplyForm input[name=projectName]').val(item.projectName);
	        			$('#financeApplyForm input[name=pCostClassName]').val(item.pCostClassName);
	        			$('#financeApplyForm input[name=costClassName]').val(item.costClassName);
	        			$('#financeApplyForm select[name=expendType]').val(item.expendType);
	        			$('#financeApplyForm select[name=isAdjustment]').val(item.isAdjustment);
	        			$('#financeApplyForm input[name=departmentNameT]').val(item.departmentName);
	        			$('#financeApplyForm textarea[name=content]').val(item.content);
	        			initBankName(item.payeeId);
	        			addApproveFlow(item);
	        			editor.readonly(true);
	        			editor.html(data.content);
	        		});
	        }
		});
	}
	 function addApproveFlow(result){
		 var applyId = result.applyId;
		 $('#approveFlow').html('');
		 console.log(ctx + "/accraditation/getApproveFlow/"+applyId);
		 var html='<ul class="timeline">';
		 html+='<li class="timeline-node"><a class="btn btn-palegreen">当前</a></li>'
		 $.ajax({
				url: ctx + "/accraditation/getApproveFlow/"+applyId,
				dataType: 'json',
				async:true,
				success: function(data){
					var toggle = true;
					var length = data.length;
					if(result.approveNextDuty){//下次审批职位不为空查询审批人
						var sysData = selectUserByDutyId(result.approveNextDuty);
				       	 $(sysData).each(function(i,item){
							 html+='<li>';
							 html+='<div class="timeline-datetime">';
							 html+='</div>';
							 html+='<div class="timeline-badge sky pass-ing">';   
					         html+='<i class="handle-num pass-content">'+(length+2)+'</i>';
					         html+='</div>';
					         html+='<div class="timeline-panel">';
					         html+='<div class="timeline-header bordered-bottom bordered-blue">';
					         html+='<p class="timeline-datetime">';
					         html+='<small class="text-muted">';
					         html+='<i class="glyphicon glyphicon-time"></i>';
					         html+='</small>';
					         html+='</p>';
					         html+='</div>';
					         html+=' <div class="timeline-body">';
					         html+='<a href="#"></a> <a href="#">审批人:'+(item.realName+('<br/><br/>审批状态:待审批'))+'</a>';
					         html+='<div class="tl-wide text-center" style="padding: 5px; margin-top:10px; margin-bottom: 10px;"></div>';
					         html+='<i class="text-muted text-sm"></i>';
					         html+='</div>';	
					         html+='</div>';
					         html+='</li>';
							 return false;
						 });
				  }
				
			        length = length+1;
					$(data).each(function(i,item){
						if(i==0&&result.approveNextDuty==null){
							html+=comepleteHtml=completeText(result,item);
						}
						if(toggle){
							html+='<li class="timeline-inverted">';
							toggle=false;
						}else{
							html+='<li class="">';
							toggle=true;
						}
						
						html+='<div class="timeline-datetime">';
						html+='<span class="timeline-time">'+(item.createDate.split(' ')[1])+'</span>';
						html+='<span class="timeline-date">'+(item.createDate.split(' ')[0])+'</span>';
						html+='</div>';
						if(item.approveStatus=='0'){
							html+='<div class="timeline-badge unpass">';
						}else{
							html+='<div class="timeline-badge pass">';
						}
						   
                        html+='<i class="handle-num pass-content">'+(length--)+'</i>';
                        html+='</div>';
                        html+='<div class="timeline-panel">';
                        html+='<div class="timeline-header bordered-bottom bordered-blue">';
                        html+='<p class="timeline-datetime">';
                        html+='<small class="text-muted">';
                        html+='<i class="glyphicon glyphicon-time"></i>';
                        html+='<span class="timeline-date">'+(item.createDate.split(' ')[1])+'</span>';
                        html+='<span class="timeline-time">'+(item.createDate.split(' ')[0])+'</span>';
                        html+='</small>';
                        html+='</p>';
                        html+='</div>';
                        html+=' <div class="timeline-body">';
                        html+='<a href="#"></a> <a href="#">审批人:'+(item.approveUserName+'<br/><br/>审批状态:'+(item.approveStatus=='0'?'未通过':'已通过'))+'</a>';
                        html+='<div class="tl-wide text-center" style="padding: 5px; margin-top:0px; margin-bottom: 10px;"></div>';
                        var approveHtml = "";
                        var splitlength = Math.ceil(item.approveAdvice.length/8);
                        var locationInit = 4;
                        for(var i=0;i<splitlength;i++){
                        	if(i==0){
                        		approveHtml+=item.approveAdvice.substring(0,locationInit);
                        	}else{
                        		approveHtml+='<br/>'+item.approveAdvice.substring(locationInit,locationInit+8);
                        		locationInit=locationInit+8;
                        	}
                        }
                        html+='<p class="text-muted text-sm">'+('审批意见:')+approveHtml+'</p>';
                        html+='</div>';	
                        html+='</div>';
                        html+='</li>';
					});
					 if(toggle){
						 html+='<li class="timeline-inverted">';
					 }else{
						 html+='<li>';
					 }
					 html+='<div class="timeline-datetime">';
					 html+='<span class="timeline-time">'+(result.createDate.split(' ')[1])+'</span>';
					 html+='<span class="timeline-date">'+(result.createDate.split(' ')[0])+'</span>';
					 html+='</div>';
					 html+='<div class="timeline-badge sky pass-ing">';   
			         html+='<i class="handle-num pass-content">1</i>';
			         html+='</div>';
			         html+='<div class="timeline-panel">';
			         html+='<div class="timeline-header bordered-bottom bordered-blue">';
			         html+='<p class="timeline-datetime">';
			         html+='<small class="text-muted">';
			         html+='<i class="glyphicon glyphicon-time"></i>';
                     html+='<span class="timeline-date">'+(result.createDate.split(' ')[1])+'</span>';
                     html+='<span class="timeline-time">'+(result.createDate.split(' ')[0])+'</span>';
			         html+='</small>';
			         html+='</p>';
			         html+='</div>';
			         html+=' <div class="timeline-body">';
			         html+='<a href="#"></a> <a href="#">申请人:'+(result.applicantName+('<br/><br/>申请状态:提出申请'))+'</a>';
			         html+='<div class="tl-wide text-center" style="padding: 5px; margin-top:10px; margin-bottom: 10px;"></div>';
			         html+='<i class="text-muted text-sm"></i>';
			         html+='</div>';	
			         html+='</div>';
			         html+='</li>'
	 				 html+="</ul>";
	 				$('#approveFlow').html(html);
				}
		 				
			});
		
	 }
	
	function initBankName(payeeId){//银行相关信息
		$.ajax({
	        url: ctx + '/bizFinance/load',
	        data:{"financePayeeId":payeeId},
	        type: 'POST',
	        dataType: 'json',
	        success: function (data) {
	        	data = data.returnObject.aaData;
	        	$(data).each(function(i,item){
	        		$('#financeApplyForm input[name=payeeId]').val(item.accountName);
	        		$('#financeApplyForm input[name=bankName]').val(item.bankName);
        			$('#financeApplyForm input[name=province]').val(item.province);
        			$('#financeApplyForm input[name=city]').val(item.city);
	        	});
	        }
		});
	}
	function selectUserByDutyId(dutyId){
		var sysData="";
		$.ajax({
	        url: ctx + '/user/selectUserByDutyId',
	        data:{"dutyId":dutyId},
	        type: 'POST',
	        async:false,
	        dataType: 'json',
	        success: function (data) {
	        	sysData = data.list;
	        }
		});
		return sysData;
	}
   function completeText(applyResult,lastResult){
	    var text="";
	   	text+='<li>';
	   	text+='<div class="timeline-datetime">';
	   	text+='<span class="timeline-time">'+(lastResult.createDate.split(' ')[1])+'</span>';
	   	text+='<span class="timeline-date">'+(lastResult.createDate.split(' ')[0])+'</span>';
	   	text+='</div>';
	   	text+='<div class="timeline-badge sky pass-ing">';   
	   	text+='<i class="pass-content">完成</i>';
	   	text+='</div>';
	   	text+='<div class="timeline-panel">';
	   	text+='<div class="timeline-header bordered-bottom bordered-blue">';
	   	text+='<p class="timeline-datetime">';
	   	text+='<small class="text-muted">';
	   	text+='<i class="glyphicon glyphicon-time"></i>';
	   	text+='<span class="timeline-date">'+(lastResult.createDate.split(' ')[1])+'</span>';
	   	text+='<span class="timeline-time">'+(lastResult.createDate.split(' ')[0])+'</span>';
	   	text+='</small>';
	   	text+='</p>';
	   	text+='</div>';
	   	text+=' <div class="timeline-body">';
	   	text+='<a href="#"></a> <a href="#">申请人:'+(applyResult.applicantName+('<br/><br/>申请状态:审批完成<br/><br/>审批状态:'+(applyResult.applicantStatus=='1'?'已通过':'未通过')))+'</a>';
	   	text+='<div class="tl-wide text-center" style="padding: 5px; margin-top:10px; margin-bottom: 10px;"></div>';
	   	text+='<i class="text-muted text-sm"></i>';
	   	text+='</div>';	
	   	text+='</div>';
	   	text+='</li>';
	   	return text;
   } 
	initApplyInfo();
})(jQuery)